What are Razor Pages in ASP.NET Core 6 MVC?

您所在的位置:网站首页 what is razor in mvc What are Razor Pages in ASP.NET Core 6 MVC?

What are Razor Pages in ASP.NET Core 6 MVC?

2024-07-16 18:46| 来源: 网络整理| 查看: 265

Razor Pages is an ASP.NET Core technology that simplifies the web application programming and development process. This model simplifies the process by integrating specific common patterns and coding components into the Razor technology that would otherwise need to be coded separately in controllers or views.

Things such as automatic model binding can be inherent in Razor Pages if coded properly. Additionally, these pages use Tag Helpers which are a facilitator between front-end HTML elements and server-side code. Razor Pages adopt a file-based routing approach as each view equates to an endpoint. All of this makes development using Razor Pages powerful and easier.

ASP.NET Core supports Razor Pages, but ASP.NET does not.

Enabling Razor Pages in an MVC Web Application

If you walked through our tutorial Create an ASP.NET Core MVC Website with Login and a Custom SQL Server Database, you will see that Razor Pages are enabled automatically in the Program.cs file when we allow Visual Studio to create our base MVC project.

Open the Program.cs file that is in the root directory of your DemoProject project and scroll to the bottom of the code. There you will see the following code

app.MapRazorPages(); app.Run();

app.MapRazorPages() add endpoints for Razor Pages to the IEndpointRouteBuilder. In turn, IEndpointRouteBuilder defines a contract for a route builder in an application. A route builder specifies the routes for an application.

You can add the following to the Program.cs file to add services for Razor Pages to your web application.

builder.Services.AddRazorPages(); What Does a Razor Page Look Like?

Navigate to the LearnASPNET.com Contact page on this website. The full code for that Razor page is below.

@model LearnASPNET.Models.EmailContact @{ ViewData["Title"] = ViewData["Heading1"] = ViewData["MetaDescription"] = "Contact Learn ASP.NET"; ViewData["ogimage"] = "https://www.learnaspnet.com/images/default.jpg"; } @using AspNetCore.ReCaptcha @Html.ReCaptcha() @ViewBag.Message

At the top of the page (below), within curly braces, is where you can place additional C# code.

@{ ViewData["Title"] = ViewData["Heading1"] = ViewData["MetaDescription"] = "Contact Learn ASP.NET"; ViewData["ogimage"] = "https://www.learnaspnet.com/images/default.jpg"; }

In the Model-View-Controller architecture pattern, Razor Pages use a controller which retrieves data, calls views, and handles browser requests (get and puts) among other things.

[ValidateReCaptcha] [HttpPost] public IActionResult Contact(EmailContact model) { if (ModelState.IsValid) { //Read SMTP settings from AppSettings.json. string host = this.Configuration.GetValue("EmailConfiguration:SmtpServer"); int port = this.Configuration.GetValue("EmailConfiguration:Port"); string fromAddress = this.Configuration.GetValue("EmailConfiguration:FromAddress"); string userName = this.Configuration.GetValue("EmailConfiguration:UserName"); string password = this.Configuration.GetValue("EmailConfiguration:Password"); using (MailMessage mm = new MailMessage(fromAddress, "[email protected]")) { mm.Subject = model.Subject; mm.Body = "Name: " + model.Name + "Email: " + model.Email + "" + model.Body; mm.IsBodyHtml = true; using (SmtpClient smtp = new SmtpClient()) { smtp.Host = host; smtp.EnableSsl = false; NetworkCredential NetworkCred = new NetworkCredential(userName, password); smtp.Credentials = NetworkCred; smtp.Port = port; smtp.Send(mm); ViewBag.Message = "Email sent sucessfully."; } } } return View(); }

In the Model-View-Controller architecture pattern, Razor Pages use a model or class representing the data the application manages. This class interacts with the Razor page to bind data, handle required fields, define label names, validate fields, and much, much more. 

using System.ComponentModel; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using RequiredAttribute = System.ComponentModel.DataAnnotations.RequiredAttribute; namespace LearnASPNET.Models { public class EmailContact { [Required] [DisplayName("Your Name:")] public string Name { get; set; } [Required] [DisplayName("Subject:")] public string Subject { get; set; } [Required] [DataType(DataType.EmailAddress)] [DisplayName("Your Email:")] public string Email { get; set; } [NotMapped] [Required] [DataType(DataType.EmailAddress)] [Compare("Email", ErrorMessage = "Email Addresses do not match.")] [DisplayName("Confirm Your Email:")] public string ConfirmEmail { get; set; } [Required] [DisplayName("Your Message:")] public string Body { get; set; } //public IFormFile Attachment { get; set; } } } Conclusion

Using the Razor Page web application programming model simplifies the web application programming and development process by incorporating technology that would otherwise need to be coded separately in controllers or views.



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3